<?php

namespace App\Http\Controllers;

use App\Models\Didi;
use App\Models\JTK;
use App\Models\PlatformCallback;
use App\Models\Task;
use App\Models\UserAmountLog;
use App\Models\UserTask;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;

class TaskController extends Controller
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function list(Request $request)
    {
        $uid = $request->user()->id;
        $type = (int)$request->input('type', Task::TYPE_XS);

        if ($type == Task::TYPE_SJ) {
            return $this->listSJ($request);
        }

        $type = [$type];

        $list = Task::query()
            ->where('status', \App\Models\Task::STATUS_OPEN)
            ->whereIn('type', $type)
            ->orderByDesc('type')
            ->get();

        $userTask = UserTask::query()->where('user_id', $uid)->whereIn('task_id', $list->pluck('id'))
            ->whereBetween('created_at', [date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59')])
            ->orderByDesc('updated_at')->get()->groupBy('task_id');
        foreach ($list as $value) {
            $value->user_number = isset($userTask[$value->id]) ? $userTask[$value->id]->count() : 0;
            $value->user_task_status = isset($userTask[$value->id]) ? $userTask[$value->id]->first()->status : 0;
            if ($value->user_task_status == UserTask::STATUS_X) {
                $value->user_number = 0;
            }
        }

        return $this->success($list);
    }

    private function listSJ(Request $request)
    {
        $uid = $request->user()->id;

        $list = Task::query()
            ->where('status', \App\Models\Task::STATUS_OPEN)
            ->where('type', Task::TYPE_SJ)
            ->get();

        $didi = new Didi();
        $jtk = new JTK();
        foreach ($list as $value) {
            $value->platform_data = [];
            $source_id = $value->id . '-' . $uid;
            if ($value->platform == Task::PLATFORM_DIDI && $value->platform_id) {
                $value->platform_data = $didi->generate($source_id, $value->platform_id);
            } elseif ($value->platform == Task::PLATFORM_JTK && $value->platform_id) {
                $value->platform_data = $jtk->generate($source_id, $value->platform_id);
            }
        }

        $userTask = UserTask::query()->where('user_id', $uid)->whereIn('task_id', $list->pluck('id'))
            ->orderByDesc('updated_at')->get()->groupBy('task_id');
        foreach ($list as $value) {
            $value->user_number = isset($userTask[$value->id]) ? $userTask[$value->id]->count() : 0;
            $value->user_task_status = isset($userTask[$value->id]) ? $userTask[$value->id]->first()->status : 0;
        }

        return $this->success($list);
    }

    public function save(Request $request)
    {
        $uid = $request->user()->id;
        $task_id = (int)$request->input('id');
        $content = (string)$request->input('content', '');
        if (!$task_id) {
            return $this->error('参数错误');
        }
        $task = Task::query()->find($task_id);
        if (!$task) {
            return $this->error('任务不存在');
        }

        $userTask = UserTask::query()->create([
            'user_id' => $uid,
            'task_id' => $task_id,
            'content' => $content,
        ]);
        if ($task->type == Task::TYPE_XS && $task->action != Task::ACTION_DY) {
            $count = UserTask::query()
                ->where(['user_id' => $uid, 'task_id' => $task_id])
                ->whereBetween('created_at', [date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59')])
                ->count();
            if ($count == $task->number) {
                $user = \App\Models\User::query()->find($uid);
                $user->amount += $task->award_number;
                $user->save();

                UserAmountLog::query()->create(
                    [
                        'user_id' => $uid,
                        'amount' => $task->award_number,
                        'balance' => $user->amount,
                        'type' => 0,
                        'map_id' => $userTask->id,
                        'remark' => '悬赏任务',
                    ]
                );
            }
        } else {
            //暂时没用
            if ($task->action == Task::ACTION_GG) {
                $user = \App\Models\User::query()->find($uid);
                $user->amount += $task->award_number;
                $user->save();
            }
        }

        return $this->success();
    }

    public function callback(Request $request)
    {
        if ($request->get('platform', 0) == Task::PLATFORM_DIDI) {
            $result = Didi::callback($request->all());
            return response()->json($result);
        } else if ($request->get('platform', 0) == Task::PLATFORM_JTK) {
            $result = JTK::callback($request->all());
            echo 'success';
            die;
        }
        return response()->json();
    }
}
